package com.jhgsys.internal.accident.service.impl;

import com.jhgsys.internal.accident.dto.AccidentReportDto;
import com.jhgsys.internal.accident.dto.AccidentReportReasonDto;
import com.jhgsys.internal.accident.dto.AccidentReportRectificationDto;
import com.jhgsys.internal.accident.entity.AccidentReportRectification;
import com.jhgsys.internal.accident.mapper.AccidentReportRectificationMapper;
import com.jhgsys.internal.accident.service.IAccidentReportRectificationService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zjb
 * @date 2024-09-13 10:18:53
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentReportRectificationServiceImpl extends ServiceImpl<AccidentReportRectificationMapper, AccidentReportRectification> implements IAccidentReportRectificationService {

    /**
     * 查询（分页）
     * @author zjb
     * @date 2024-09-13 10:18:53
     * @param request QueryRequest
     * @param accidentReportRectification accidentReportRectification
     * @return IPage<AccidentReportRectification>
    */
    @Override
    public IPage<AccidentReportRectification> findAccidentReportRectificationPage(QueryRequest request, AccidentReportRectification accidentReportRectification) {
        LambdaQueryWrapper<AccidentReportRectification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportRectification::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentReportRectification> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zjb
     * @date 2024-09-13 10:18:53
     * @param accidentReportRectification accidentReportRectification
     * @return List<AccidentReportRectification>
     */
    @Override
    public List<AccidentReportRectification> findAccidentReportRectificationList(AccidentReportRectification accidentReportRectification) {
	    LambdaQueryWrapper<AccidentReportRectification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportRectification::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zjb
    * @date 2024-09-13 10:18:53
    * @param id
    * @return AccidentReportRectification
    */
    @Override
    public AccidentReportRectification findAccidentReportRectificationById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zjb
     * @date 2024-09-13 10:18:53
     * @param accidentReportRectification accidentReportRectification
     */
    @Override
    @Transactional
    public boolean createAccidentReportRectification(AccidentReportRectification accidentReportRectification) {
        return this.save(accidentReportRectification);
    }

    /**
     * 修改
     * @author zjb
     * @date 2024-09-13 10:18:53
     * @param accidentReportRectification accidentReportRectification
     */
    @Override
    @Transactional
    public boolean updateAccidentReportRectification(AccidentReportRectification accidentReportRectification) {
        return this.updateById(accidentReportRectification);
    }

    /**
     * 逻辑删除
     * @author zjb
     * @date 2024-09-13 10:18:53
     * @param accidentReportRectification accidentReportRectification
     * @param accidentReportRectificationIds accidentReportRectificationIds
     */
    @Override
    @Transactional
    public boolean deleteAccidentReportRectification(AccidentReportRectification accidentReportRectification,String accidentReportRectificationIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(accidentReportRectificationIds.split(StringPool.COMMA));

        accidentReportRectification.setDeletedBy(user.getUserName());
        accidentReportRectification.setDeletedTime(new Date());
        accidentReportRectification.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(accidentReportRectification,new LambdaQueryWrapper<AccidentReportRectification>().in(AccidentReportRectification::getReportRectificationId,list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author zjb
    * @date 2024-09-13 10:18:53
    * @param accidentReportRectificationIds accidentReportRectificationIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteAccidentReportRectification(String accidentReportRectificationIds) {
        List<String> list = Arrays.asList(accidentReportRectificationIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportRectification> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentReportRectification::getReportRectificationId,list);
        return this.remove(wapper);
    }

    @Override
    public IPage<AccidentReportRectificationDto> findPageByReport(QueryRequest request, AccidentReportDto accidentReportDto) {
        Page<AccidentReportRectificationDto> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.baseMapper.findPageByReport(page,accidentReportDto);
    }
}
